﻿using Blog.Model;
using Blog.Utility.ApiResult;
using IBaseService;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Blog.Controllers
{
	[Route("api/[controller]")]
	[ApiController]
	[Authorize]
	public class ArticleTypeController : ControllerBase
	{
		private readonly IArticleTypeService _articleTypeService;

		public ArticleTypeController(IArticleTypeService articleTypeService)
		{
			_articleTypeService = articleTypeService;
		}

		[HttpGet("ArticleTypeList")]
		public async Task<IActionResult> QueryList()
		{
			var list = await _articleTypeService.QueryAsync();
			return Ok(list);
		}

		[HttpPost("CreateArticleType")]
		public async Task<ActionResult<ApiResult>> CreateArticle(string typeName)
		{
			ArticleType articleType = new ArticleType()
			{
				TypeName = typeName,
			};

			bool res = await _articleTypeService.CreateAsync(articleType);
			if (!res) return ApiResultTool.Fail("新增失败");
			return ApiResultTool.Success(articleType);
		}

		[HttpPost("DeleteArticleType")]
		public async Task<ActionResult<ApiResult>> DeleteArticle(int id)
		{
			bool res = await _articleTypeService.DeleteByIdAsync(id);
			if (!res) return ApiResultTool.Fail("删除失败");
			return ApiResultTool.Success(res);
		}

		[HttpPost("UpdateArticleType")]
		public async Task<ActionResult<ApiResult>> UpdateArticle(int id, string typeName)
		{
			ArticleType articleType = await _articleTypeService.FindByIdAsync(id);
			if (articleType == null) return ApiResultTool.Fail("没有找到该文章类型");
			articleType.TypeName = typeName;

			bool res = await _articleTypeService.EditAsync(articleType);
			if (!res) return ApiResultTool.Fail("修改失败");
			return ApiResultTool.Success(res);
		}
	}
}
